﻿import com.dinmax.isolution.*

class HCC
{
	private static var devices:Array=new Array();
	private static var themes:Array=new Array();
	
	
	public static function updateDevice(device:Device) {
		
		for(var i:Number=0;i<devices.length;i++) {
			if (devices[i].getId()==device.getId()) {
				devices[i].setState(device.getState());
				devices[i].setGroup(device.getGroup());
				devices[i].setName(device.getName());
				return;
			}
		}
		
		devices.push(device);
		return;
	};
	
	public static function updateTheme(theme:Theme) {
		for(var i:Number=0;i<themes.length;i++) {
			if (themes[i].getId()==theme.getId()) {
				themes[i].setIcon(theme.getIcon());
				themes[i].setIndex(theme.getIndex());
				themes[i].setName(theme.getName());
				themes[i].setFavorite(theme.getFavorite());
				return;
			}
		}
		
		themes.push(theme);
		return;
	};
	
	public static function getDevice(id:String):Device {
		for(var i:Number=0;i<devices.length;i++) {
			if (devices[i].getId()==id) {				
				return devices[i];
			}
		}
	};
	
	public static function getTheme(id:String):Theme {
		for(var i:Number=0;i<themes.length;i++) {
			if (themes[i].getId()==id) {				
				return themes[i];
			}
		}
	};

	public static function getDevices():Array {
		return devices;
	};
	
	public static function getThemes():Array {
		return themes;
	};

	public static function findThemes() {
		Communication.getInstance().sendAction(new ActionGetThemes());
		
	};
	
	public static function findDevices() {
		Communication.getInstance().sendAction(new ActionGetArtifact());
	};
	
	public static function setDeviceState(device:String, state:String) {
		var dev:Device=getDevice(device);
		//TODO esto es para parchar lo de la luz
		if(dev.getId() == "fakeRGB"){
			dev.setState(state);
			_root.sendRGB(dev.getState());
			
			var act = new ActionChangeDevice(
				dev.getId(),
				dev.getName(),
				dev.getGroup(),
				dev.getState()
			);
					
			act.addAttribute("type", "rgb");
			
			Communication.getInstance().onXML(new XML(act.toXML()));
			
		} else {
			dev.setState(state);
			updateServerDevice(dev);
		}
		return;
	};
	
	
	public static function setPerson(person:Person):Void {
		var act = new ActionSetPerson(
			person.getId(),
			person.getName(),
			person.getLastName()
		);
		
		Communication.getInstance().sendAction(act);
		return;
	};
	
	public static function launchTheme(id:String):Void {
		
		//TODO esto es para las escenas locas
		trace("HCC launchTheme id="+id);
		
		var dev:Device=getDevice("fakeRGB");
							
		switch(id){
			case "0":
			_root.sendRGB("255,255,53");
			dev.setState("255,255,53");
			Communication.getInstance().sendAction(new ActionLaunchTheme(id));
			break;
			
			case "1":
			_root.sendRGB("75,0,90", 80);
			dev.setState("75,0,90");
			Communication.getInstance().sendAction(new ActionLaunchTheme(id));
			break;
			
			case "2":
			_root.sendRGB("255,255,211");
			dev.setState("255,255,211");
			Communication.getInstance().sendAction(new ActionLaunchTheme(id));
			break;
			
			case "3":
			_root.sendRGB("255,255,255");
			dev.setState("255,255,255");
			Communication.getInstance().sendAction(new ActionLaunchTheme(id));
			break;
			
			case "100":
			HCC.dummyScenes(id);
			break;
			
			case "101":
			HCC.dummyScenes(id);
			break;
			
			case "102":
			HCC.dummyScenes(id);
			break;
			
			case "103":
			HCC.dummyScenes(id);
			break;
		}
		
		var act = new ActionChangeDevice(
			dev.getId(),
			dev.getName(),
			dev.getGroup(),
			dev.getState()
		)
		
		act.addAttribute("type", "rgb");
		
		Communication.getInstance().onXML(new XML(act.toXML()));
		
		return;
	};
	
	// TODO esto está re choto
	public static function updateServerTheme(theme:Theme) {
		var act = new ActionChangeTheme(
			theme.getId(),
			theme.getName(),
			theme.getIcon(),
			theme.getFavorite()
		);
		
		Communication.getInstance().sendAction(act);
		
		return;
	};
			
	// TODO esto está re choto
	public static function setDeviceName(device:String,name:String) {
		var dev:Device=getDevice(device);
		dev.setName(name);
		updateServerDeviceData(dev);
		return;
	};
	
	// TODO esto está re choto
	public static function setDeviceGroup(device:String,group:String) {
		var dev:Device=getDevice(device);
		dev.setGroup(group);
		updateServerDeviceData(dev);
		return;
	};
	
	
	private static function updateServerDevice(device:Device):Void {
		var act = new ActionChangeDevice(
				device.getId(),
				device.getName(),
				device.getGroup(),
				device.getState()
			);
		
		// TODO esto es para probar sin Matías
		if(Communication.DUMMY){
			var sType:String;
			if(device instanceof LightDimmer) sType = "dimmer";
			if(device instanceof LightRGB) sType = "rgb";
			if(device instanceof Door) sType = "door";
			if(device instanceof Blind) sType = "blind";
			
			act.addAttribute("type", sType);
		};
		//
		
		Communication.getInstance().sendAction(act);
		
		return;
	};
	
	// TODO esta funcion no debería existir
	private static function updateServerDeviceData(device:Device):Void {
		
		var act = new ActionChangeDevice(
			device.getId(),
			device.getName(),
			device.getGroup()
		);
		
		// TODO esto es para probar sin Matías
		if(Communication.DUMMY){
			var sType:String;
			if(device instanceof LightDimmer) sType = "dimmer";
			if(device instanceof LightRGB) sType = "rgb";
			if(device instanceof Door) sType = "door";
			if(device instanceof Blind) sType = "blind";
			
			act.addAttribute("type", sType);
		};
		//
		
		Communication.getInstance().sendAction(act);
		
		return;
	};
	
	//TODO esto es para simular las escenas
	private static function dummyScenes(id:String):Void {
		
		var dev:Device=getDevice("fakeRGB");
				
		switch(id){
			case "100":
			_root.sendRGB("255,255,255", 500);
			dev.setState("255,255,255")
			break;
			
			case "101":
			_root.sendRGB("0,0,0", 240);
			dev.setState("0,0,0");
			break;
			
			case "102":
			_root.sendRGB("0,45,60", 240);
			dev.setState("0,45,60");
			break;
			
			case "103":
			HCC.crazyRGB();
			dev.setState("0,0,0");
			break;
		}
		
		var act = new ActionChangeDevice(
			dev.getId(),
			dev.getName(),
			dev.getGroup(),
			dev.getState()
		)
		
		act.addAttribute("type", "rgb");
		
		Communication.getInstance().onXML(new XML(act.toXML()));
		
		var mcInterpolator:MovieClip = _root["dummyScenes"];
		if(!mcInterpolator) _root.createEmptyMovieClip("dummyScenes", _root.getNextHighestDepth());
				
		return;
	};
	
	//TODO simula la luz loca rgb
	private static function crazyRGB():Void {
		var color1:Object = {r:255, g:90, b:255};
		var color2:Object = {r:13, g:224, b:79};
		var color3:Object = {r:255, g:255, b:40};
		var color4:Object = {r:255, g:10, b:10};
		var aColors:Array = [color1, color2, color3, color4];
		var nColor:Number = 0;
		var nTimer:Number = getTimer();
		var nDif:Number = 1500;
		
		var mcRGB:MovieClip = _root["crazyRGB"];
		if(!mcRGB) mcRGB = _root.createEmptyMovieClip("crazyRGB", _root.getNextHighestDepth());
		
		var oColor:Object = aColors[aColors.length-1];
		_root.sendRGB(oColor.r+","+oColor.g+","+oColor.b, 15, true);
				
		mcRGB.onEnterFrame = function():Void {
			if(getTimer() - nTimer > nDif){
				nTimer = getTimer();
				var oColor:Object = aColors[nColor];
				
				nColor ++;
				if(nColor > (aColors.length - 1)) nColor = 0;
				
				_root.sendRGB(oColor.r+","+oColor.g+","+oColor.b, 15, true);
			}
			return;
		}
		
		return;
	}
	
	
	
}